package com.nativetalk.service.three.study.search;

import com.nativetalk.base.Page;
import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.study.TdNtStudyTeachListNew;
import com.nativetalk.bean.study.TdNtStudyTeachTeacherListNew;
import com.nativetalk.common.Constants;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.I18nMessages;
import com.nativetalk.dao.TdNtStudyTeachDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 16/8/26.
 */
@Service
public class ThreeStudySearchServiceImpl implements ThreeStudySearchService{
    private static Logger logger = Logger.getLogger(ThreeStudySearchServiceImpl.class);
    @Resource
    private TdNtStudyTeachDao tdNtStudyTeachDao;

    @Override
    public RetInfo findStudyTeachPage(Page page) {
        String logInfo = this.getClass().getName() + ":findStudyTeachPage:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {

            int totalPage;//总页数
            int currentPage = page.getCurrentPage();
            int pageSize = page.getPageSize();
            int totalRecord = tdNtStudyTeachDao.findTdNtStudyTeachByMembIdSize(Integer.valueOf(page.getObj().toString()));//总条数
            if (totalRecord % pageSize == 0) {
                totalPage = totalRecord/pageSize;
            } else {
                totalPage = (totalRecord/pageSize) + 1;
            }
            if (currentPage >= totalPage) {
                currentPage = totalPage;
            }
            if (currentPage <= 1) {
                currentPage = 1;
            }
            page.setCurrentPage((currentPage-1)*10);

            List<TdNtStudyTeachListNew> tdNtStudyTeachListNews = tdNtStudyTeachDao.findTdNtStudyTeachNewByMembIdPage(page);
            if (tdNtStudyTeachListNews == null || tdNtStudyTeachListNews.size() <= 0) {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("没有学习记录");
            } else {

                Map<String, Object> result = new HashMap<>();
                result.put("studys", tdNtStudyTeachListNews);
                result.put("totalPage", totalPage);
                result.put("teacher", Constants.teacher);
                retInfo.setObj(result);
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取学习记录成功");
            }

        } catch (Exception e) {
            retInfo.setMark("-1");
            retInfo.setTip("json数据错误");
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo findStudyTeachTeacherPage(Page page) {
        String logInfo = this.getClass().getName() + ":findStudyTeachTeacherPage:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {

            int totalPage;//总页数
            int currentPage = page.getCurrentPage();
            int pageSize = page.getPageSize();
            int totalRecord = tdNtStudyTeachDao.findTdNtStudyTeachByTeaIdSize(Integer.valueOf(page.getObj().toString()));//总条数
            if (totalRecord % pageSize == 0) {
                totalPage = totalRecord/pageSize;
            } else {
                totalPage = (totalRecord/pageSize) + 1;
            }
            if (currentPage >= totalPage) {
                currentPage = totalPage;
            }
            if (currentPage <= 1) {
                currentPage = 1;
            }
            page.setCurrentPage((currentPage-1)*10);

            List<TdNtStudyTeachTeacherListNew> tdNtStudyTeachListNews = tdNtStudyTeachDao.findTdNtStudyTeachNewByTeaIdPage(page);
            if (tdNtStudyTeachListNews == null || tdNtStudyTeachListNews.size() <= 0) {
                Map<String, Object> result = new HashMap<>();
                result.put("studys", tdNtStudyTeachListNews);
                result.put("totalPage", totalPage);
                result.put("member", Constants.member);
                retInfo.setObj(result);
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip(I18nMessages.en.getString("no.content.to.show"));
            } else {

                Map<String, Object> result = new HashMap<>();
                result.put("studys", tdNtStudyTeachListNews);
                result.put("totalPage", totalPage);
                result.put("member", Constants.member);
                retInfo.setObj(result);
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip(I18nMessages.en.getString("msg.success"));
            }
        } catch (Exception e) {
            retInfo.setMark("-1");
            retInfo.setTip("json数据错误");
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
